'''
Author: ZhangRui
Date: 2023-03-06 10:02:17
LastEditors: ZhangRui
LastEditTime: 2023-03-10 14:29:34
FilePath: \get-goods-info\login.py
Description: 
'''
# -*- coding: utf-8 -*-
# @Author   : MXcl
# @Time     : 2023/2/4 13:43
# @Description : 自动登录淘宝
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.webdriver import WebDriver


class LoginTaobao:
    driver: WebDriver = None

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.init_driver()

    def init_driver(self):

        option = webdriver.ChromeOptions()
        option.add_experimental_option("detach", True)
        # option.add_argument("--disable-blink-features")
        # 隐藏selenium的自动自动控制的功能的显示防止被检测
        option.add_argument("--disable-blink-features=AutomationControlled")
        # 设置修改selenium的特征值，防止被检测到
        option.add_experimental_option(
            'excludeSwitches', ['enable-automation'])

        self.driver = webdriver.Chrome(
            "C:\Program Files\Google\Chrome\Application\chromedriver.exe", options=option)
        self.driver.maximize_window()  # 设置浏览器打开全屏
        print("初始化浏览器完成~")

    def __find_login_page(self):
        print("跳转到登录页")
        self.driver.get("https://www.taobao.com/")
        self.driver.implicitly_wait(3)
        login_ = self.driver.find_element(
            By.XPATH, "//*[@id='J_SiteNavLogin']/div[1]/div[1]/a[1]")
        login_.click()
        sleep(3)
        self.driver.switch_to.window(self.driver.window_handles[-1])
        self.driver.implicitly_wait(3)
        pwd_login = self.driver.find_element(
            By.XPATH, "//*[@id='login']/div[2]/div/div[1]/a[1]")
        pwd_login.click()
        sleep(2)

    def write_login_info(self):
        self.__find_login_page()
        print("输入用户名和密码")
        username_text = self.driver.find_element(
            By.XPATH, '//*[@id="fm-login-id"]')
        password_text = self.driver.find_element(
            By.XPATH, '//*[@id="fm-login-password"]')
        username_text.send_keys(self.username)
        password_text.send_keys(self.password)

    def slider_validation(self):
        try:
            self.driver.switch_to.frame("baxia-dialog-content")
            sleep(2)
            slider = self.driver.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
            print("滑块验证")
        except Exception as e:
            return False
        slider_container = self.driver.find_element(
            By.XPATH, '//*[@id="nc_1__scale_text"]/span')
        move_offset = slider_container.size["width"] - slider.size["width"]
        webdriver.ActionChains(self.driver).drag_and_drop_by_offset(
            slider, move_offset, 0).perform()
        sleep(2)

    def login(self):
        self.write_login_info()
        login_button = self.driver.find_element(
            By.XPATH, '//*[@id="login-form"]/div[4]/button')
        login_button.click()
        sleep(2)
        self.slider_validation()
        print("成功登录")


# def main():
#     LoginTaobao(..., ...).login()


# if __name__ == "__main__":
#     main()
